package com.webchat.chat.core.tools;

import com.corundumstudio.socketio.BroadcastOperations;
import com.corundumstudio.socketio.SocketIOClient;
import com.webchat.chat.core.dto.evenMessage.EventInfo;
import com.webchat.chat.core.enums.EventType;
import com.webchat.chat.core.enums.SocketClientType;

import java.util.Date;

/**
 * Created by Owner on 2018/6/27.
 */
public class SocketEventUtil {
    /**
     * Protect from being instance.
     */
    private SocketEventUtil() {
    }

    private static EventInfo base(String eventName, String clientId, SocketClientType socketClientType, String userName,
                                  Date date, Object content, EventType type) {
        EventInfo eventInfo = new EventInfo();
        eventInfo.setContent(content);
        eventInfo.setEventDesc(eventName);
        eventInfo.setTargetClientId(clientId);
        eventInfo.setTargetClientType(socketClientType);
        eventInfo.setTargetUserName(userName);
        eventInfo.setTime(date);
        eventInfo.setType(type);
        return eventInfo;
    }

    private static void roomEventBase(BroadcastOperations room, String eventName, String clientId,
                                      SocketClientType socketClientType, String userName, Date date, Object content) {
        EventInfo eventInfo = base(eventName, clientId, socketClientType, userName, date, content, EventType.房间事件);
        room.sendEvent(eventName, eventInfo);
    }

    private static void eventBase(SocketIOClient client, String eventName, String clientId,
                                  SocketClientType socketClientType, String userName, Date date, Object content) {
        EventInfo eventInfo = base(eventName, clientId, socketClientType, userName, date, content, EventType.个人事件);
        client.sendEvent(eventName, eventInfo);
    }

    /**
     * SocketConstant.CONNECT_FAIL_BY_NON_PARAM事件
     *
     * @param client           对已经的socketio中的客户端
     * @param clientId         redis中的客户端clientId
     * @param socketClientType 客户端类型
     * @param userName         用户名
     */
    public static void connectFailByNonParam(SocketIOClient client, String clientId, SocketClientType socketClientType, String userName) {
        //缺少必要信息
        //发送消息告知客户端
        eventBase(client, SocketConstant.CONNECT_FAIL_BY_NON_PARAM, clientId, socketClientType, userName, new Date(), "缺少必要数据,已断开连接");
    }

    /**
     * 客户离开事件
     *
     * @param userName
     * @param c
     * @param targetClientId
     * @param targetClientType
     */
    public static void customerLeave(SocketIOClient client, String userName, String targetClientId, SocketClientType targetClientType) {
        eventBase(client, SocketConstant.CUSTOMER_LEAVE, targetClientId, targetClientType, targetClientId, new Date(), "[" + userName + "]客户已离开!");
    }

    /**
     * 客户离开事件
     *
     * @param userName
     * @param targetClientId
     */
    public static void customerLeave(BroadcastOperations room, String userName, String targetClientId) {
        roomEventBase(room, SocketConstant.CUSTOMER_LEAVE, targetClientId, SocketClientType.客服, userName, new Date(), "[" + userName + "]离开房间!");
    }

    /**
     * 无可用客服事件
     *
     * @param userName
     * @param client
     * @param clientId
     */
    public static void notExistAvailableCS(SocketIOClient client, String userName, String clientId) {
        eventBase(client, SocketConstant.NOT_EXIST_AVAILABLE_CS, clientId, SocketClientType.客户, userName, new Date(), "客服全忙,请稍等!");
    }

    /**
     * 目标对象不存在事件
     *
     * @param client
     * @param sourceClientId
     * @param userName
     * @param clientType
     * @param targetUserName
     */
    public static void targetNotExist(SocketIOClient client, String sourceClientId, String userName, SocketClientType clientType, String targetUserName) {
        eventBase(client, SocketConstant.TARGET_NOT_EXIST_EVENT, sourceClientId, clientType, userName, new Date(), "[" + targetUserName + "]用户不存在,消息发送失败!");
    }

    /**
     * 连接不存在事件
     *
     * @param client
     * @param clientId
     * @param userName
     * @param clientType
     */
    public static void socketConnectNotExist(SocketIOClient client, String clientId, String userName, SocketClientType clientType) {
        eventBase(client, SocketConstant.CLIENT_CONNECT_INFO_NOT_EXIST, clientId, clientType, userName, new Date(), "用户连接失败!");
    }

    /**
     * 房间不存在事件
     *
     * @param client
     * @param clientId
     * @param userName
     * @param clientType
     */
    public static void socketRoomNotExist(SocketIOClient client, String clientId, String userName, SocketClientType clientType) {
        eventBase(client, SocketConstant.SOCKET_ROOM_NOT_EXIST, clientId, clientType, userName, new Date(), "未联系到客服!");
    }

    /**
     * 目标对象不在线
     *
     * @param client
     * @param sourceClientId
     * @param userName
     * @param clientType
     * @param targetUserName
     */
    public static void targetOffline(SocketIOClient client, String sourceClientId, String userName, SocketClientType clientType, String targetUserName) {
        eventBase(client, SocketConstant.TARGET_OFFLINE_EVENT, sourceClientId, clientType, userName, new Date(), "[" + targetUserName + "]用户不在线,消息发送失败!");
    }

    /**
     * 消息发送成功事件
     *
     * @param client
     * @param dataId
     * @param sourceClientId
     * @param userName
     * @param clientType
     */
    public static void msgSendSuccess(SocketIOClient client, String dataId, String sourceClientId, String userName, SocketClientType clientType) {
        eventBase(client, SocketConstant.MESSAGE_SEND_SUCCESS, sourceClientId, clientType, userName, new Date(), "[" + dataId + "]消息发送成功!");
    }

    /**
     * 消息发送失败事件
     *
     * @param client
     * @param dataId
     * @param sourceClientId
     * @param userName
     * @param clientType
     */
    public static void msgSendFail(SocketIOClient client, String dataId, String sourceClientId, String userName, SocketClientType clientType) {
//        eventBase(client, SocketConstant.MESSAGE_SEND_FAIL, sourceClientId, clientType, userName, new Date(), "[" + dataId + "]消息发送失败!");
        eventBase(client, SocketConstant.MESSAGE_SEND_FAIL, sourceClientId, clientType, userName, new Date(), dataId);
    }

    /**
     * 消息发送失败事件--缺少必要参数
     *
     * @param client
     * @param dataId
     * @param sourceClientId
     * @param userName
     * @param clientType
     */
    public static void msgSendFailByNonParam(SocketIOClient client, String dataId, String sourceClientId, String userName, SocketClientType clientType) {
        eventBase(client, SocketConstant.SEND_MSG_FAIL_BY_NON_PARAM, sourceClientId, clientType, userName, new Date(), "[" + dataId + "]消息因为缺少必要参数导致发送失败!");
    }

    /**
     * 客服进入房间
     *
     * @param room
     * @param targetClientId
     * @param userName
     */
    public static void csIntoRoom(BroadcastOperations room, String targetClientId, String userName) {
        roomEventBase(room, SocketConstant.CS_INTO_ROOM, targetClientId, SocketClientType.客户, userName, new Date(), "[" + userName + "]: 您好，请问有什么需要帮助您的吗？");
    }


    /**
     * 客户进入聊天室
     *
     * @param room
     * @param targetClientId
     * @param userName
     */
    public static void cIntoPublicRoom(BroadcastOperations room, String targetClientId, String userName) {
        roomEventBase(room, SocketConstant.JION_PUBLIC_CHAT_ROOM, targetClientId, SocketClientType.客户, userName, new Date(), "[" + userName + "]进入房间!");
    }









    /**
     * 告知客服房间中的信息
     *
     * @param client
     * @param targetClientId
     * @param roomInfo
     * @param userName
     */
    public static void tellCSRoomInfo(SocketIOClient client, String targetClientId, String userName, Object roomInfo) {
        eventBase(client, SocketConstant.SOCKET_ROOM_INFO, targetClientId, SocketClientType.客服, userName, new Date(), roomInfo);
    }

    /**
     * 告知客服邀请成功
     *
     * @param client
     * @param targetClientId
     * @param userName
     */
    public static void csInviteSuccess(SocketIOClient client, String targetClientId, String userName) {
        eventBase(client, SocketConstant.SOCKET_ROOM_INFO, targetClientId, SocketClientType.客服, userName, new Date(), "[" + userName + "]进入房间");
    }

    /**
     * 告知客服邀请失败
     *
     */
    public static void csInviteFail(SocketIOClient client, String targetClientId, String userName) {
        eventBase(client, SocketConstant.SOCKET_ROOM_INFO, targetClientId, SocketClientType.客服, userName, new Date(), "[" + userName + "]进入房间失败");
    }

    /**
     * 客服结束对话事件
     *
     */
    public static void endChat(BroadcastOperations room, String targetClientId, String userName) {
        roomEventBase(room, SocketConstant.END_CHAT, targetClientId, SocketClientType.客户, userName, new Date(), "客服" + userName +"已退出房间");
    }

    /**
     * 客服离开房间事件
     *
     */
    public static void leaveRoom(SocketIOClient client, String targetClientId, String userName) {
        eventBase(client, SocketConstant.LEAVE_ROOM, targetClientId, SocketClientType.客户, userName, new Date(), "您已退出房间");
    }

    /**
     * 客服转接成功事件
     *
     */
    public static void transferSuccess(SocketIOClient client, String targetClientId, String userName) {
        eventBase(client, SocketConstant.TRANSFER_SUCCESS, targetClientId, SocketClientType.客服, userName, new Date(), "转接成功");
    }

    /**
     * 客服转接失败事件
     *
     */
    public static void transferFail(SocketIOClient client, String targetClientId, String userName) {
        eventBase(client, SocketConstant.TRANSFER_FAIL, targetClientId, SocketClientType.客服, userName, new Date(), "待转接客服接待数量超过最大值，转接失败");
    }

}
